// 商品数据
const products = [
    {
        id: 1,
        name: "智能手机 X1",
        price: 1999,
        image: "images/product1.jpg",
        description: "高性能智能手机，6.5英寸屏幕",
        category: "electronics"
    },
    {
        id: 2,
        name: "无线蓝牙耳机",
        price: 299,
        image: "images/product2.jpg",
        description: "高音质无线蓝牙耳机",
        category: "electronics"
    },
    {
        id: 3,
        name: "男士休闲衬衫",
        price: 159,
        image: "images/product3.jpg",
        description: "纯棉男士休闲衬衫",
        category: "clothing"
    },
    {
        id: 4,
        name: "JavaScript高级程序设计",
        price: 99,
        image: "images/product4.jpg",
        description: "前端开发必备书籍",
        category: "books"
    },
    {
        id: 5,
        name: "智能电饭煲",
        price: 399,
        image: "images/product5.jpg",
        description: "多功能智能电饭煲",
        category: "home"
    },
    {
        id: 6,
        name: "进口牛奶",
        price: 69,
        image: "images/product6.jpg",
        description: "新西兰进口纯牛奶",
        category: "food"
    },
    {
        id: 7,
        name: "笔记本电脑",
        price: 5999,
        image: "images/product7.jpg",
        description: "高性能轻薄笔记本电脑",
        category: "electronics"
    },
    {
        id: 8,
        name: "女士连衣裙",
        price: 199,
        image: "images/product8.jpg",
        description: "夏季新款女士连衣裙",
        category: "clothing"
    }
];

// 页面加载完成后执行
document.addEventListener('DOMContentLoaded', () => {
    // 从 URL 获取商品 ID
    const urlParams = new URLSearchParams(window.location.search);
    const productId = parseInt(urlParams.get('id'));

    if (!productId) {
        window.location.href = 'index.html';
        return;
    }

    // 根据商品 ID 查找商品信息
    const product = products.find(p => p.id === productId);

    if (!product) {
        window.location.href = 'index.html';
        return;
    }

    // 填充商品信息
    document.getElementById('productCategory').textContent = product.category;
    document.getElementById('productName').textContent = product.name;
    document.getElementById('mainImage').src = product.image;
    document.getElementById('productTitle').textContent = product.name;
    document.getElementById('productDescription').textContent = product.description;
    document.getElementById('productPrice').textContent = `¥${product.price.toFixed(2)}`;
    document.querySelectorAll('.add-to-cart, .buy-now').forEach(button => {
        button.setAttribute('data-id', product.id);
    });

    // 添加购物车事件
    document.querySelectorAll('.add-to-cart').forEach(button => {
        button.addEventListener('click', addToCart);
    });

    // 立即购买事件
    document.querySelectorAll('.buy-now').forEach(button => {
        button.addEventListener('click', buyNow);
    });
});

// 添加到购物车
function addToCart(event) {
    const productId = parseInt(event.target.getAttribute('data-id'));
    const product = products.find(p => p.id === productId);
    
    if (!product) return;
    
    let cart = JSON.parse(localStorage.getItem('cart')) || [];
    const existingItem = cart.find(item => item.id === productId);
    
    if (existingItem) {
        existingItem.quantity += 1;
    } else {
        cart.push({
            id: product.id,
            name: product.name,
            price: product.price,
            image: product.image,
            quantity: 1
        });
    }
    
    localStorage.setItem('cart', JSON.stringify(cart));
    updateCartCount();
    
    // 显示添加成功提示
    const notification = document.createElement('div');
    notification.className = 'notification';
    notification.textContent = `${product.name} 已添加到购物车`;
    document.body.appendChild(notification);
    
    setTimeout(() => {
        notification.classList.add('show');
        setTimeout(() => {
            notification.classList.remove('show');
            setTimeout(() => {
                document.body.removeChild(notification);
            }, 300);
        }, 2000);
    }, 10);
}

// 立即购买
function buyNow(event) {
    const productId = parseInt(event.target.getAttribute('data-id'));
    const product = products.find(p => p.id === productId);
    
    if (!product) return;
    
    let cart = JSON.parse(localStorage.getItem('cart')) || [];
    const existingItem = cart.find(item => item.id === productId);
    
    if (existingItem) {
        existingItem.quantity += 1;
    } else {
        cart.push({
            id: product.id,
            name: product.name,
            price: product.price,
            image: product.image,
            quantity: 1
        });
    }
    
    localStorage.setItem('cart', JSON.stringify(cart));
    updateCartCount();
    
    // 跳转到结算页
    window.location.href = 'checkout.html';
}